<template>
  <div>
    <div class="header">
      <view class="bg">
        <view class="box">
          <view class="box-hd" style="margin-bottom: 30rpx">
            <!-- <view class="avator">
              <img src="../../../static/icon/correct.png" />
            </view> -->
            <button
              class="avatar-wrapper"
              open-type="chooseAvatar"
              @chooseavatar="onChooseAvatar"
            >
              <image class="avatar" :src="avatarUrl"></image>
            </button>
            <!-- <view class="phone-number">{{ phone }}</view> -->
          </view>
          <view class="box-bd">
            <view class="item">
              <view class="icon">
                <!-- <img src="../../../static/icon/tongZhi.png"/> -->
                <van-icon
                  v-if="infoLen"
                  name="chat"
                  size="65rpx"
                  color="#fd5f29"
                  :info="infoLen"
                />
                <van-icon v-else name="chat" size="65rpx" color="#fd5f29" />
              </view>
              <view class="text">我的通知</view>
            </view>
            <view class="item" @click="handleKaoQin">
              <view class="icon"
                ><img src="../../../static/icon/kaoqin.png"
              /></view>
              <view class="text">我的考勤</view>
            </view>
            <view class="item" @click="handleDaFen">
              <view class="icon"
                ><img src="../../../static/icon/kaohedafen.png"
              /></view>
              <view class="text">我的打分</view>
            </view>
          </view>
        </view>
      </view>
    </div>
    <view class="list-content">
      <view class="list">
        <view class="li" @click="handleLogup">
          <!-- <view class="icon">
            <img src="../../../static/icon/userInfo.png" />
          </view> -->
          <view class="text">退出登录</view>
          <img class="to" src="../../../static/icon/right.png" />
        </view>
        <!-- <view class="li">
          <view class="icon">
            <img src="../../../static/icon/sure.png" />
          </view>
          <view class="text">我的考勤</view>
          <img class="to" src="../../../static/icon/right.png" />
        </view>
        <view class="li">
          <view class="icon">
            <img src="../../../static/icon/daFen.png" />
          </view>
          <view class="text">我的打分</view>
          <img class="to" src="../../../static/icon/right.png" />
        </view> -->
      </view>
      <soure :url="url"></soure>
    </view>
    <van-dialog id="van-dialog" />
  </div>
</template>
<script>
import { getUserProfile } from "@/utils/api/userInfo";
import { searchXinChou } from "@/utils/api/sure.js";
import { getNotice } from "@/utils/api/home.js";
import moment from "moment";
import Dialog from "../../../static/vant/dialog/dialog";
export default {
  data() {
    const defaultAvatarUrl =
      "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";
    return {
      url: "https://ext.dcloud.net.cn/plugin?id=223",
      avatarUrl: defaultAvatarUrl,
      phone: "",
      infoLen: 0,
    };
  },
  onLoad() {},
  onShow() {
    this.userCode = wx.getStorageSync("usercode");
    const yyyy_mm_dd = moment().format("YYYY-MM");
    this.searchXinChou(yyyy_mm_dd);
  },
  mounted() {
    this.getUserProfile();
  },
  methods: {
    onChooseAvatar(e) {
      this.avatarUrl = e.mp.detail.avatarUrl;
      wx.uploadFile({
        url: "https://www.openrzcl.com/prod-out-api/system/user/profile/avatar",
        filePath: e.mp.detail.avatarUrl,
        name: "avatarfile",
        header: {
          "Content-type": "multipart/form-data",
          Authorization: wx.getStorageSync("token"),
        },
        success: (res) => {
          // console.log("upload succeed");
          if (res.code == 200) {
            this.getUserProfile();
          }
          // var data = res.data;
        },
        fail: () => {
          console.log("upload failed");
        },
      });
    },
    handleKaoQin() {
      wx.navigateTo({
        url: "/pages/kaoqin/main",
      });
    },
    handleDaFen() {
      wx.navigateTo({
        url: "/pages/grade/main",
      });
    },
    handleLogup() {
      Dialog.confirm({
        title: "退出登录",
        message: "您将退出登录，是否继续？",
      })
        .then(() => {
          // on confirm
          // removeStorage
          wx.clearStorage({
            success: function (res) {
              // success
              wx.showToast({
                title: `退出登录成功！`,
                icon: "none",
              });
              wx.redirectTo({
                url: "/pages/login/main",
              });
            },
          });
        })
        .catch(() => {
          // on cancel
          console.log(555666);
        });
    },
    async getUserProfile() {
      try {
        let res = await getUserProfile();
        if (res.code == 200) {
          this.avatarUrl =
            "https://www.openrzcl.com/prod-out-api" + res.data.avatar;
          this.phone = res.data.phonenumber;
        }
      } catch (error) {
        console.log(error);
      }
    },
    async searchXinChou(data) {
      try {
        let res = await searchXinChou(data);
        if (res.code == 200) {
          if (res.data) {
            this.getNotice({
              monthId: res.data[0].id,
              userCode: this.userCode,
            });
          }
        }
      } catch (error) {
        console.log(error);
      }
    },
    async getNotice(data) {
      try {
        let res = await getNotice(data);
        if (res.code == 200) {
          if (res.data) {
            this.infoLen = res.data.length;
          } else {
            this.infoLen = 0;
          }
        }
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style lang="less">
page {
  background-color: #f1f1f1;
  font-size: 30rpx;
}

.header {
  background: #fff;
  height: 290rpx;
  padding-bottom: 110rpx;

  .bg {
    width: 100%;
    height: 200rpx;
    padding-top: 100rpx;
    background-color: #4191ea;
  }
}

.box {
  width: 650rpx;
  height: 280rpx;
  border-radius: 20rpx;
  margin: 0 auto;
  background: #fff;
  box-shadow: 0 5rpx 20rpx 0rpx rgba(0, 0, 150, 0.2);

  .box-hd {
    display: flex;
    flex-wrap: wrap;
    flex-direction: row;
    justify-content: center;
    .avatar-wrapper {
      width: 160rpx;
      height: 160rpx;
      border-radius: 8px;
      background-color: #fff;
      margin-top: -80rpx;
      overflow: hidden;
      .avatar {
        width: 160rpx;
        height: 160rpx;
        margin-left: -26rpx;
      }
    }

    .avator {
      width: 160rpx;
      height: 160rpx;
      background: #fff;
      border: 5rpx solid #fff;
      border-radius: 50%;
      margin-top: -80rpx;
      overflow: hidden;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .phone-number {
      width: 100%;
      text-align: center;
    }
  }

  .box-bd {
    display: flex;
    flex-wrap: nowrap;
    flex-direction: row;
    justify-content: center;

    .item {
      flex: 1 1 auto;
      display: flex;
      flex-wrap: wrap;
      flex-direction: row;
      justify-content: center;
      border-right: 1px solid #f1f1f1;
      margin: 15rpx 0;

      &:last-child {
        border: none;
      }

      .icon {
        width: 60rpx;
        height: 60rpx;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .text {
        width: 100%;
        text-align: center;
        margin-top: 10rpx;
      }
    }
  }
}

.list-content {
  background: #fff;
}

.list {
  width: 100%;
  border-bottom: 15rpx solid #f1f1f1;
  background: #fff;

  &:last-child {
    border: none;
  }

  .li {
    width: 92%;
    height: 100rpx;
    padding: 0 4%;
    border-bottom: 1px solid rgb(243, 243, 243);
    display: flex;
    align-items: center;

    &.noborder {
      border-bottom: 0;
    }

    .icon {
      flex-shrink: 0;
      width: 50rpx;
      height: 50rpx;

      img {
        width: 50rpx;
        height: 50rpx;
      }
    }

    .text {
      padding-left: 20rpx;
      width: 100%;
      color: #666;
    }

    .to {
      flex-shrink: 0;
      width: 40rpx;
      height: 40rpx;
    }
  }
}
</style>

